package com.example.demo.controller;

import com.example.demo.main.ShowCart;
import com.example.demo.service.UserService;
import com.example.demo.until.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mizili
 * @Description 加密和解密
 * @create 2020/7/15 14:47
 * Copyright: Copyright (c) 2020
 * Company:XXXXXXXX
 */
@Controller
public class EncryptionController {

    //保存私钥
    private static RSAPrivateKey privateKey = null;


    @Autowired
    private UserService userService;

    /**
     * 生成公钥和私钥
     *
     * @param username
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/rsaKey/{username}")
    public Map<String, String> generateRSAKey(@PathVariable String username) {
        // 获取公钥和私钥
        HashMap<String, Object> keys = null;
        try {
            keys = RSAUtils.getKeys();
            RSAPublicKey publicKey = (RSAPublicKey) keys.get("public");
            privateKey = (RSAPrivateKey) keys.get("private");
            // 将公钥传到前端
            Map<String, String> map = new HashMap<String, String>();
            // 注意返回modulus和exponent以16为基数的BigInteger的字符串表示形式
            map.put("modulus", publicKey.getModulus().toString(16));
            map.put("exponent", publicKey.getPublicExponent().toString(16));
            return map;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证密码
     *
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/rsaKey/{username}/{password}")
    public String checkRSAKey(@PathVariable String username, @PathVariable String password, HttpServletRequest request) {
        // 解密
        try {
            String realPWD = RSAUtils.decryptByPrivateKey(password, privateKey);
            if (userService.checkNamePWd(username, realPWD)) {
                ShowCart showCart = userService.showCartPriceAndCount(username);
                request.getSession().setAttribute("cartCount", showCart.getCount());
                request.getSession().setAttribute("cartPrice", showCart.getPrice());
                request.getSession().setAttribute("userName", username);
                request.getSession().setAttribute("loginUrl", "/User/userdetail");
                return "yes";
            }
            return "用户名或密码错误!!";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "登录失败!!";
    }

}
